#include "Variant.h"

Variant::Variant( void )
: size( 0 ), type( _unk ), is_array( false ), data( NULL )
{
}

Variant::Variant( int size, Type type, bool is_array, void* data )
: size( size ), type( type ), is_array( is_array )
{
	this->data = new char[size];
	memcpy( this->data, data, size );
}

Variant::Variant( const Variant& that )
{
	data = new char[that.size];
	memcpy( data, that.data, that.size );
	size = that.size;
	type = that.type;
	is_array = that.is_array;
}

Variant::~Variant( void )
{
	clear();
}

Variant& Variant::operator=( const Variant& that )
{
	if ( this == &that )
	{
		return *this;
	}

	clear();
	data = new char[that.size];
	memcpy( data, that.data, that.size );
	size = that.size;
	type = that.type;

	return *this;
}

void Variant::clear( void )
{
	if ( data == NULL )
	{
		return;
	}

	switch ( type )
	{
	case _unk: throw MyException::Type::unk_variant_type();
	case _int: _clear< int >(); break;
	}
}

template< class T >
void Variant::_clear( void )
{
	if ( is_array )
	{
		delete[]( static_cast< T* >( data ) );
		return;
	}
	delete( static_cast< T* >( data ) );

	size = 0;
	type = _unk;
	is_array = false;
}